#include "Task.hh"
#include <algorithm>
#include <cmath>

using namespace std;
using namespace World;

Planet::Task::Task(QString const& name, u8 amount):
    name(name),
    amount(amount) {}

/**
 * \brief Feladat végrehajtása.
 * \param planet Melyik bolygón.
 * \return Befejeződött-e a feladat.
 */
bool Planet::Task::execute(Planet& planet) {
    u8 const workCost = work_cost();
    u8 const moneyCost = money_cost();
    u8 itemCount = amount;

    if (workCost > 0) {
        work += planet.industry_production();
        itemCount = min(work / workCost, itemCount);
    }

    if (moneyCost > 0) {
        money += planet.get_owner()->transaction(itemCount * moneyCost);
        itemCount = min(money / moneyCost, itemCount);
    }

    if (itemCount > 0) {
        amount -= itemCount;
        work -= itemCount * workCost;
        money -= itemCount * moneyCost;
        build(planet, itemCount);
    }

    return amount <= 0;
}

QString Planet::Task::get_name() const {
    return name;
}

u8 Planet::Task::get_amount() const {
    return amount;
}
